/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display {
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.service.singleton.MouseService;
	import com.googlecode.milib.vo.MLEvent;

	import flash.display.DisplayObject;

	/**
	 * @author Marek Brun
	 */
	public class MCDragModel extends MLBroadcastClass {
		
		public const event_PreStartInit : String = 'PreStartInit';		public const event_Start : String = 'Start';
		public const event_NewDragXY : String = 'NewDragXY';
		public const event_Stop : String = 'Stop';
		
		protected var display:DisplayObject;
		private var isDraged:Boolean;
		private var mouseMinusX:Number;
		private var mouseMinusY:Number;
		private var ms:MouseService;
		private var iniPosX:Number;
		private var iniPosY:Number;

		public function MCDragModel(display:DisplayObject) {
			this.display=display;
			isDraged=false;
			ms=MouseService.getInstance();
		}
		
		public function start():void{
			broadcastEvent(event_PreStartInit);
			iniPosX=display.x;			iniPosY=display.y;
			mouseMinusX=display.parent.mouseX-display.x;
			mouseMinusY=display.parent.mouseY-display.y;
			ms.addEventListener(ms.event_Move, onMouseMove);			ms.addEventListener(ms.event_Up, onMouseUp);
			isDraged=true;
			broadcastEvent(event_Start);
		}
		
		public function stop():void{
			ms.removeEventListener(ms.event_Move, onMouseMove);
			ms.removeEventListener(ms.event_Up, onMouseUp);
			isDraged=false;
			broadcastEvent(event_Stop);
		}
		
		public function getX():Number{
			if(isDraged){ return display.parent.mouseX-mouseMinusX; }
			else{  return display.x; }
		}
		
		public function getY():Number{
			if(isDraged){ return display.parent.mouseY-mouseMinusY; }
			else{  return display.y; }
		}
		
		/**
		 * @return returns the diffrence between start and actual drag position in x axis
		 */
		public function getVX():Number{
			if(isDraged){ return getX()-iniPosX; }
			else{ return 0; }
		}
		
		/**
		 * @return returns the diffrence between start and actual drag position in y axis
		 */
		public function getVY():Number{
			if(isDraged){ return getY()-iniPosY; }
			else{ return 0; }
		}
		
//****************************************************************************
// EVENTS for MCDragModel
//****************************************************************************
		public function onMouseMove(event:MLEvent):void {
			broadcastEvent(event_NewDragXY);
		}
		
		public function onMouseUp(event:MLEvent):void{
			stop();
		}
		
	}
}
